#include <iostream>
#define MAXN 1005
#define MW 35
using namespace std;
int T, W;
int tree[MAXN];
int dp[MAXN][MW][3];
void solve();

int main() {
    cin >> T >> W;
    for (int i = 1; i <= T; i++)
        cin >> tree[i];
    solve();
}

void solve() {
    for (int i = 1; i <= T; i++) {
        int t = tree[i];
        for (int j = W; j >= 0; j--) {
            dp[i][j][t] = max(dp[i - 1][j][t], dp[i - 1][j + 1][3 - t]) + 1;
            dp[i][j][3 - t] = dp[i - 1][j][3 - t];
        }
    }
    int res = 0;
    for (int i = 0; i <= W; i++) {
        res = max(res, max(dp[T][i][2], dp[T][i][1]));
    }
    cout << res << endl;
}